Main Analysis (Exploratory Data Analysis)
Visualizing marketshare split of cryptocurrencies
library(coinmarketcapr)
library(treemap)
market_today <- get_marketcap_ticker_all()
df1 <- na.omit(market_today[,c('id','market_cap_usd')])
df1$market_cap_usd <- as.numeric(df1$market_cap_usd)
df1$formatted_market_cap <- paste0(df1$id,'\n','$',format(df1$market_cap_usd,big.mark = ',',scientific = F, trim = T))
treemap(df1, index = 'formatted_market_cap', vSize = 'market_cap_usd', title = 'Cryptocurrency Market Cap', fontsize.labels=c(12, 8), palette='RdYlGn')
The area of each tile represents the proportion of marketcap that particular cryptocurrency holds. Color blind friendly colors were used only to distinguish between currencies. We observe from the above tree map that as of today, more than 50% of the Cryptocurrency market cap is occupied by Bitcoin, Ethereum, Ripple, Bitcoin-cash and EOS. We visualise them further, how have they evolved over the past 5 years.
Relative market cap distributions for top 5 cryptocurrencies over 2013-present
library(crypto)
library(dplyr)
library(lubridate)
library(ggplot2)
library(memisc)
Bitcoin <- getCoins("BTC")
Ethereum <- getCoins("ETH")
Ripple <- getCoins("XRP")
Bitcoin_cash <- getCoins("BCH")
Eos <- getCoins("EOS")
data <- merge(Bitcoin,Ethereum,all = TRUE)
data <- merge(data,Ripple,all = TRUE)
data <- merge(data,Bitcoin_cash,all = TRUE)
data <- merge(data,Eos,all = TRUE)
grp_year <- data %>% group_by(name,yr = year(date)) %>% summarise(avg_cap = mean(market))
ggplot(grp_year)+ geom_bar(aes(x=yr,y=avg_cap,fill = name),stat="identity",position = position_dodge())
From the above barplot, we can conclude that all cryptocurrencies have been performing well from 2013 until now as their respective market cap has been increasing over the consecutive years. Some trends we observe here are:- * Bitcoin and Ripple have been present in the market since 2013 * Ethereum enters to the cryptocurrency market in 2015 * Bitcoin-cash and EOS have been very new additions to the crypto market since August 2017 and June 2017 resepectively. Bitcoin Cash split the main Bitcoin blockchain creating a diversion in blockchains in mid 2017. * (Is this necessary?) EOS was started by Dan Larimer the founder and creator of Bitshares as well as Steem. It was first announced by Dan at the Consensus 2017 with an odd ICO launch * While the marketcap of all of them has been growing consistently, bitcoins’ growth has been exponential.
Plotting the recent Bitcoin trends(Jan 2017 - present)
Bitcoin <- getCoins("BTC")
Bitcoin$year <- substring(Bitcoin$date,1,4)
bitcoin_events <- Bitcoin[Bitcoin$year == "2017" | Bitcoin$year == "2018", ]
plot(bitcoin_events$date,bitcoin_events$close, type = "l",ylab = "Bitcoin Close price" , xlab = "Jan 2017-April 2018", main = "Recent Bitcoin price trends")
abline(v=as.Date("2017-11-12"),col = "black",lty=2)
pt1 <- bitcoin_events[bitcoin_events$date == "2017-11-12","close"]
dt1 <- as.Date("2017-11-12")
abline(v=as.Date("2017-12-16"),col = "black",lty=2)
pt2 <- bitcoin_events[bitcoin_events$date == "2017-12-16","close"]
dt2 <- as.Date("2017-12-16")
abline(v=as.Date("2018-02-06"),col = "black",lty=2)
pt3 <- bitcoin_events[bitcoin_events$date == "2018-02-06","close"]
dt3 <- as.Date("2018-02-06")
x1 <- c(as.Date("2017-11-12"),as.Date("2017-12-16"))
y1 <- c(pt1,pt2)
x2 <- c(as.Date("2017-12-16"),as.Date("2018-02-06"))
y2 <- c(pt2,pt3)
lines(x1,y1,col="green",lwd = 2)
lines(x2,y2,col="red",lwd = 2)
text(x1[1],y1[1],y1[1],pos = 1,cex=0.5)
text(x1[2],y1[2],y1[2],pos = 4,cex=0.5)
text(x2[2],y2[2],y2[2],pos = 1,cex=0.5)
legend(as.Date("2017-01-01"),15000, legend=c("Bitcoin price increases by 300%", "Steepest fall in history"),col=c("green", "red"), lty=1:2, cex=0.8)
axis(side=3, at=as.Date("2017-11-12"), labels="12 Nov",cex.axis = 0.7)
axis(side=3, at=as.Date("2017-12-16"), labels="16 Dec",cex.axis = 0.7)
axis(side=3, at=as.Date("2018-02-06"), labels="02 Feb",cex.axis = 0.7)
On a macro level, bitcoin’s price trajectory has been influenced by increased online chatter, the enthusiasm of Asian investors for the cryptocurrency, and the coming start date for bitcoin futures. Institutional traders, who are the main clients for futures trading, are expected to increase liquidity and price stability for the cryptocurrency.
The run-up in bitcoin price from November 2017 to December 2017 was largely construed as excitement over the trading at CBOE (December 10,2017) and CME (December 18,2017). Futures trading is also a prelude to wider mainstream acceptance of bitcoin as a store of value.
The crash in Bitcoin’s price from December 2017 until February 2018 can be accounted by many factors -
South Korea’s clampdown of bitcoin has been a significant factor in its sharp value decline. It demanded that the veil of secrecy surrounding bitcoin transactions be removed as the government planned to only allow accounts with a users’ real-name to be used in transactions.The country’s customs service said that it had uncovered cryptocurrency crimes worth 637.5 billion won ($594.35 million).
Facebook said Tuesday that it was banning ads that promote cryptocurrencies. The social networking giant said it is trying to stop the promotion of “financial products and services frequently associated with misleading or deceptive promotional practices.
The U.S. Securities and Exchange Commission said it had obtained a court order to halt and freeze the assets of what is likely the largest initial coin offering ever.
Source :- https://www.investopedia.com/news/what-was-behind-bitcoins-insane-price-moves-dec-7/ https://www.cnbc.com/2018/01/31/bitcoin-is-heading-for-its-biggest-monthly-decline-since-january-2015.html
library(forecast)
library(tidyquant) # Loads tidyverse, tidyquant, financial pkgs, xts/zoo
#get bitcoin data
btc <- read.csv("btc.csv")
btc$Date <- as.Date(btc$Date,format="%d-%b-%y")
#convert bitcoin data to xts object
btc[,2:7] <- apply(apply(btc[,2:7], 2, gsub, patt=",", replace=""), 2, as.numeric)
btc.xts <- as.xts(btc[,2:7], order.by = btc$Date)
#get stock data
x <- getSymbols("^GSPC", src = "yahoo", from = min(btc$Date), to = max(btc$Date))
#join bitcoin and stock data using outer join of dates
whole = merge(btc.xts,GSPC,join = "outer")
Volume = whole[,c("Volume","GSPC.Volume")]
colnames(Volume) <- c("Bitcoin", "S&P 500")
#volume of bitcoin vs stock
plot(Volume, legend.loc = "topleft", auto.legend=TRUE)
First we plot a measure of volume. The red line is the volume of assets bought and sold in the S&P. The black line is the volume of bitcoin assets bought and sold. Our dataset contained no volume data for bitcoin prior to 2014. The volume of assets traded in the stock market (S&P) is quite variable, but is consistently within a band or range of values through 2013-2018. Bitcoin, on the other hand, is operating on very low volumes compared to the stock market until mid 2017 when the volume skyrocketed. The volume went up to be over 5 times the volume of the stock market, and it has since then declined to levels similar to those of the S&P. Gaps visible in the red line correspond to weekends or holidays, during which the stock market is closed for trading.
#closing price of bitcoin vs stock
ClosingPrice = whole[,c("Close","GSPC.Close")]
colnames(ClosingPrice) <- c("Bitcoin", "S&P 500")
#closing price of bitcoin vs stock
plot(ClosingPrice, legend.loc = "topleft", auto.legend=TRUE)
Next we plot the price, collected on a daily scale, of bitcoin and the S&P. The red line is the price of the S&P index over time, and the black line is the price of bitcoin over time. As expected, this looks similar to the volume plot. The reason the plots are similar is because in 2017, many people, many more than had done so previously, started trading bitcoin, particularly on the buy side of transactions. This accounts for the increase in volume. This also means that demand for the coins rose. And basic economics tells us that if the demand for a good or service, in this case a digital currency, rises, then the price will rise to meet demand. Hence as more people started to buy bitcoin in 2017 (more than previous years, that is), the volume of transactions rose, and the price rose with it naturally, making it considerably higher than the S&P index price. The S&P index has remained fairly steady relative to bitcoin, with a visible positive increase since 2013. The gaps in the S&P red line is again the weekends and holiays during which the stock market is closed.
#Bitcoin closingprice
closePrice.difference = whole[,"Close"] - whole[,"GSPC.Close"]
plot(closePrice.difference)
For a clearer picture of the price difference between the two assets, as discussed in the class, we look at the plotted difference. Up until Mid 2017, the S&P was price higher than bitcoin, and by about the same amount throughout. A notable exception is late 2013 when the price of bitcoin rose and the S&P price was not greatly higher than that of bitcoin. You can see the price difference reduce in late 2013 on the plot. Then in Mid 2017, the price difference reversed as it turned positive, which means that the price of bitcoin exceeded that of the S&P. The gaps also show up in this plot indicating the weekends and holidays during which the stock market was closed.
Analysis of Variance Over Time
spy <- read.csv(file="SPY.csv")
btc <- read.csv(file="btc.csv")
btc$Date <-as.Date(btc$Date, format="%d-%b-%y")
spy$Date <- as.Date(spy$date,format="%m/%d/%Y")
spy <- arrange(spy, -row_number())
index<-match('4/29/2013', spy$date)
spy <- spy[index:nrow(spy),]
spy$open <- as.numeric(spy$open)
spy$close <- as.numeric(spy$close)
spy$daily_pct_change <- spy$open/lag(spy$open) - 1
spy$Month <- months(spy$Date)
spy$Year <- format(spy$Date,format="%y")
spy$Month[spy$Month=='January'] <- 01
spy$Month[spy$Month=='February'] <- 02
spy$Month[spy$Month=='March'] <- 03
spy$Month[spy$Month=='April'] <- 04
spy$Month[spy$Month=='May'] <- 05
spy$Month[spy$Month=='June'] <- 06
spy$Month[spy$Month=='July'] <- 07
spy$Month[spy$Month=='August'] <- 08
spy$Month[spy$Month=='September'] <- 09
spy$Month[spy$Month=='October'] <- 10
spy$Month[spy$Month=='November'] <- 11
spy$Month[spy$Month=='December'] <- 12
monthly_spy <- aggregate( daily_pct_change ~ Month + Year , spy , FUN = var )
library(zoo)
monthly_spy$date <- as.yearmon(paste(monthly_spy$Year, monthly_spy$Month), "%Y %m")
monthly_spy_2 <- aggregate( daily_pct_change ~ Month, monthly_spy, FUN = mean)
ggplot(monthly_spy, aes(date, daily_pct_change)) + geom_line() + ggtitle("Variance of Daily Percentage Change, Month by Month, of S&P") + labs(x='Year(2013-2018)', y='Variance')
Here we look at how the variance of “daily percentage change” has changed over the months for S&P. The y-axis lists the years and includes the months between them. We will note what this looks like and plot the same graph for bitcoin.
btc <- arrange(btc, -row_number())
btc$Open <- as.numeric(btc$Open)
btc$Close <- as.numeric(btc$Close)
btc$daily_pct_change <- btc$Open/lag(btc$Open) - 1
#btc$abs_daily_pct_change <- abs(btc$daily_pct_change)
btc<-btc[!(btc$daily_pct_change > 10),]
In this step of proprocessing we remove all observations in which the daily percentage change was greater than 10. This accounts for fewer than 20 of the outlier observations which skews the scale of the graph.
btc$Month <- months(btc$Date)
btc$Year <- format(btc$Date,format="%y")
btc$Month[btc$Month=='January'] <- 01
btc$Month[btc$Month=='February'] <- 02
btc$Month[btc$Month=='March'] <- 03
btc$Month[btc$Month=='April'] <- 04
btc$Month[btc$Month=='May'] <- 05
btc$Month[btc$Month=='June'] <- 06
btc$Month[btc$Month=='July'] <- 07
btc$Month[btc$Month=='August'] <- 08
btc$Month[btc$Month=='September'] <- 09
btc$Month[btc$Month=='October'] <- 10
btc$Month[btc$Month=='November'] <- 11
btc$Month[btc$Month=='December'] <- 12
monthly_btc <- aggregate( daily_pct_change ~ Month + Year , btc , FUN = var )
library(zoo)
monthly_btc$date <- as.yearmon(paste(monthly_btc$Year, monthly_btc$Month), "%Y %m")
#monthly_btc
monthly_btc_2 <- aggregate( daily_pct_change ~ Month, monthly_btc, FUN = mean)
#monthly_btc_2
#btc$Date <- as.Date(btc$Date, "%d-%s-%Y")
ggplot(monthly_btc, aes(date, daily_pct_change)) + geom_line() + ggtitle("Variance of Daily Percentage Change, Monthy by Monty, of BTC") + labs(x='Year (2013-2018)', y='Variance')
Now we are looking at the variance of “daily percentage change” over the months from 2013 to 2018 for bitcoin. We see that there is a relatively smooth period between 2014 and 2017 as compared to the jagged highs of variance in the S&P index. It is interesting to note that when the S&P displays lower levels of variance than its extremes, it is still jagged and changing. But when bitcoin displays lower levels of variance, the plot is completely smooth relative to the rest of the plot. In bitcoin, these changes of variance come about when the price rises to a relatively high level in the period in which it is observed. The high variance in 2013 is due to the rise we saw above, in the graph of differences between price levels of the S&P and bitcoin.
Seasonality
library(forecast)
library(tidyquant) # Loads tidyverse, tidyquant, financial pkgs, xts/zoo
#get bitcoin data
btc <- read.csv("btc.csv")
btc$Date <- as.Date(btc$Date,format="%d-%b-%y")
#convert bitcoin data to xts object
btc[,2:7] <- apply(apply(btc[,2:7], 2, gsub, patt=",", replace=""), 2, as.numeric)
btc.xts <- as.xts(btc[,2:7], order.by = btc$Date)
#get stock data
x <- getSymbols("^GSPC", src = "yahoo", from = min(btc$Date), to = max(btc$Date))
#convert bitcoin data to xts object
btc[,2:7] <- apply(apply(btc[,2:7], 2, gsub, patt=",", replace=""), 2, as.numeric)
btc.xts <- as.xts(btc[,2:7], order.by = btc$Date)
snp_daily_return <- tail(diff(log(GSPC$GSPC.Adjusted), lag=1),-1) #dailyReturn(Ad(GSPC))
btc_daily_return <- tail(diff(log(btc.xts$Close), lag=1),-1)
b <-data.frame(date=index(btc_daily_return), return=coredata(btc_daily_return)[,1], type='Bitcoin')
s <-data.frame(date=index(snp_daily_return), return=coredata(snp_daily_return)[,1], type='S&P 500')
d <- na.omit(rbind(b,s))
grp_week <- d %>% group_by(type,week = wday(date, label=TRUE)) %>% summarise(avg_return = mean(return))
ggplot(grp_week)+ geom_bar(aes(x=week,y=avg_return),stat="identity")+ facet_wrap(~type,nrow=1)+ ylab("Average Returns")
Now we are interested in the seasonal behavior of the two assets, in all sorts. First we look at daily patterns across weeks. The above two plots look at the value of the percentage log returns of each day of the week from the day before, and that shows on the y-axis. The x-axis shows the day of the week on which the daily percentage change is recorded. As weobserved during data analysis, bitcoin includes 7 days since its market is always open, even on holidays, as it is operated in a decentralized manner. However, the stock market graph includes 5 days since it is only open on weekdays. These graphs are not easy to read as they are on different scales, so we will let the scales be free and plot the corresponding days on top of one another. However, before we do that, we notice that the daily percentage returns of the stock market are dwarfed by bitcoin. This is the reason why the scales are so “off.” Now we will change the scales.
grp_week <- d %>% group_by(type,week = wday(date, label=TRUE)) %>% summarise(avg_return = mean(return))
ggplot(grp_week)+ geom_bar(aes(x=week,y=avg_return),stat="identity")+ facet_wrap(~type,ncol=1, scales='free_y')+ylab("Average Returns")
This is the same set of graphs as above except the scale for S&P was reduced in order for the pattern among the bars to be shown. The bar patterns between the two assets do not seem to match, in fact they seem to be reversed of each other. As the week progresses starting from Monday, bitcoin’s daily returns seems to steadily decrease. At the same time, the S&P index seems to steadily increase. What this means with respect to the time frame of the week is left to the reader to decide. One thought is that the “traditional” market of the S&P has more investor enthusiasm when most people are entering the “traditional” setting of the M-F 9-5 work schedule, while thoughts about “alternative” markets such as bitcoin are forgotten. Note that this is only a hypothesis. This pattern - bitcoin returns dropping and S&P returns rising - continues until Wednesday. After that the pattern discontinues. On Friday, we see an increase in the returns of the stock market. This is attributable to people leaving work or their institutions with a purchase of stocks to end the week. For bitcoin, Friday shows a relatively low return. Bitcoin’s highest returns are on Monday and Saturday.
grp_mon <- d %>% group_by(type,mon = month(date,label=TRUE)) %>% summarise(avg_return = mean(return))
ggplot(grp_mon)+ geom_bar(aes(x=mon,y=avg_return),stat="identity")+ facet_wrap(~type,ncol=1,scales='free')+ ylab("Average Returns")+xlab("")
Now for seasonality, we look at monthly behavior across years. Each month in the x-axis shows the returns of the month from the previous month. The bitcoin returns are much higher than those of the S&P, so we let the scales be free in order to see patterns in the bar graphs. Unlike in the weeks, we now see some matching patterns. January is negative, March is low, April is low, May is high, June is low, August is low, September is low, October is high, November is high, and December is low. The only months that are different are February and July. These patterns are quite interesting. It shows that even across the highs and lows of bitcoin, particularly the recent skyrocketing rise, the two assets show some long-term correlation and investors are buying and selling them together, to an extent. More on correlation will come later.
Similarly, we analyse patterns in bitcoin and stock market volume monthly and weekly.
#convert bitcoin data to xts object
btc[,2:7] <- apply(apply(btc[,2:7], 2, gsub, patt=",", replace=""), 2, as.numeric)
btc.xts <- as.xts(btc[,2:7], order.by = btc$Date)
snp_daily_volume <- GSPC$GSPC.Volume
btc_daily_volume <- btc.xts$Volume
b <-data.frame(date=index(btc_daily_volume), volume=coredata(btc_daily_volume)[,1], type='Bitcoin')
s <-data.frame(date=index(snp_daily_volume), volume=coredata(snp_daily_volume)[,1], type='S&P 500')
d <- na.omit(rbind(b,s))
grp_week <- d %>% group_by(type,week = wday(date, label=TRUE)) %>% summarise(avg_volume = mean(volume))
ggplot(grp_week)+ geom_bar(aes(x=week,y=avg_volume),stat="identity")+ facet_wrap(~type,ncol=1)+ylab("Average Volume")
grp_mon <- d %>% group_by(type,mon = month(date, label=TRUE)) %>% summarise(avg_volume = mean(volume))
ggplot(grp_mon)+ geom_bar(aes(x=mon,y=avg_volume),stat="identity")+ facet_wrap(~type,ncol=1)+ylab("Average Volume")
The volume of S&P is more or less uniform acroos all months. Not the case with Bitcoin. But is it one year thats an outlier? Let’s facet by year and see. No weekly patetrns were observed.
d$year = year(d$date)
btc_d <- d[d$type == 'Bitcoin',]
grp_mon <- btc_d[btc_d$year != 2013,] %>% group_by(mon = month(date, label=TRUE), year) %>% summarise(avg_volume = mean(volume))
ggplot(grp_mon)+ geom_bar(aes(x=mon,y=avg_volume),stat="identity") + facet_wrap(~year,ncol=1)+ylab("Average Volume")
ggplot(grp_mon)+ geom_bar(aes(x=mon,y=avg_volume),stat="identity") + facet_wrap(~year,ncol=1,scales="free_y")+ylab("Average Volume")
No, the pattern is not consistent across months. It is only because of high volume in the beginning of 2018 and end of 2017.
Investment Returns
Now we are interested in the question of investments and returns in Bitcoin as well as the stock market (as indexed and tracked by the S&P Index). We know that bitcoin is more volatile than the stock market, but what about its returns for an investor? We look at simple investment strategies and plot their returns using a histogram. First we ask, if one buys $100 of the asset, what is their return after 30 days? What does that data look like?
spy <- read.csv(file="SPY.csv")
btc <- read.csv(file="btc.csv")
btc$Date <-as.Date(btc$Date, format="%d-%b-%y")
spy$Date <- as.Date(spy$date,format="%m/%d/%Y")
spy <- arrange(spy, -row_number())
index<-match('4/29/2013', spy$date)
spy <- spy[index:nrow(spy),]
returns <- c()
for (i in 1:nrow(btc)) {
r <- spy[i+22, 2]*100/spy[i, 2]
rr <- r - 100
returns <- c(returns, rr)
}
returns <-as.data.frame(returns)
returns <- na.omit(returns)
library(ggplot2)
ggplot(returns, aes(returns)) + geom_histogram(binwidth=1,
fill= '#56b4e9', col = '#e69f00',
alpha=0.6) + ggtitle("30 Day (22 trading) $100 Investment Returns for S&P") + labs(x='Returns', y='Frequency')
We now look the returns of investing in the stock market, or the S&P index. This histogram shows the distribution of returns under the following conditions: the investor invests $100 into the index and checks his returns 30 days in the future, which is equivalent to 22 trading days since weekends not included in the stock market. Then as you expet from a 30 day investment of $100, the returns are in the range of -10 to 10.
Facts: There were 1223 trading events looked at for this trading strategy. Essentially, every day since 2013 when our data analysis begins is counted as a buy day, and 30 days later than that is the sell day. The expected return (mean) of this investment strategy comes out to $0.94.
As expected in the stock market, the returns are approximately normally distributed. One expects this from the stock market which has been active for centuries. If the returns looked differently or skewed to one side, investment would capitalize on it and would change the distribution until the returns come out to be a normal distribution. The mean of $0.94 may seem small, but remember this is only a 30 day time period, with $100 invested. And the mean is positive because in the years covered by our dataset, 2013 to 2018, there existed a general bull market.
btc <- arrange(btc, -row_number())
btc$Close <- as.numeric(btc$Close)
returns <- c()
for (i in 1:nrow(btc)) {
r <- btc[i+30, 5]*100/btc[i, 5]
rr <- r - 100
returns <- c(returns, rr)
}
returns <-as.data.frame(returns)
returns <- na.omit(returns)
returns <- returns[returns<1000]
returns <-as.data.frame(returns)
library(ggplot2)
ggplot(returns, aes(returns)) + geom_histogram(bins=100,
fill= '#56b4e9', col = '#e69f00',
alpha=0.6) + ggtitle("30 Day $100 Investment Returns for BTC") + labs(x='Returns', y='Frequency')
Now we look at the same investment strategy applied to the bitcoin market and analyze the returns yielded. That is, we consider an investor investing $100 in bitcoin on a given day, and selling his investment for a profit or a loss 30 days later, and we look at this return. The first aspect of this graph to notice is that the range, particularly the x-scale, is much larger than the same for the S&P. In fact, the x-scale would be bigger but we limited the outliers and limited the returns to 1000 for convenience of graphing. The largest return observed here was $156,300!
Facts: There were 1739 trading events in this analysis. This is a significantly larger number than the same for the S&P, and this is because bitcoin markets are not closed in the weekends. The expected return (mean) in this investment strategy is a whopping $30.82.
The expected return is astonishingly higher than that of the S&P. This is on account of the general increase in bitcoin investments and investor sentiment between 2013 and 2018. There are many extreme values in the data. We can observe that the distribution is right skewed. Additionally, we may observe that the distribution is fat-tailed. High returns on the far right of this graph may account for much of the variance observed. Notwithstanding the outliers, the distribution looks similar to a normal distribution. Let’s take a closer look.
returns <- c()
for (i in 1:nrow(btc)) {
r <- btc[i+30, 5]*100/btc[i, 5]
rr <- r - 100
returns <- c(returns, rr)
}
returns <-as.data.frame(returns)
returns <- na.omit(returns)
returns <- returns[returns<200]
returns <-as.data.frame(returns)
library(ggplot2)
ggplot(returns, aes(returns)) + geom_histogram(bins=100,
fill= '#56b4e9', col = '#e69f00',
alpha=0.6) + ggtitle("30 Day $100 Investment Returns for BTC") + labs(x='Returns', y='Frequency')
We restrict returns to be smaller than $200 and we see a close-up of the above graph. In this subset of the data (which has 1635 trading events), the expected return is decreased from above to $2.29. We see that the distribution looks like defective sort of normal distribution with some asymmetries, but it is still said to be recognizable as a normal distribution. Another considerable observation is that there were many several instances of losses close to $100, which means that the investor would lose his original $100 plus another $100. Bad deal!
btc$daily_pct_change <- btc$Close/lag(btc$Close) - 1
avg_rate <- c()
for (i in 2:nrow(btc)) {
i <- i
r <- (btc[i, 8] + btc[i+1, 8] + btc[i+2, 8] + btc[i+3, 8] + btc[i+4, 8] + btc[i+5, 8] + btc[i+6, 8])/8
avg_rate <- c(avg_rate, r)
}
avg_rate <- avg_rate[!(is.na(avg_rate))]
avg_rate <- c(c(0, 0, 0, 0, 0, 0, 0), avg_rate)
#avg_rate <- c(c(0, 0, 0), avg_rate)
btc$avg_rate <- avg_rate
marker <- ((btc$avg_rate > 0.05))
btc$marker <- marker
length(which(btc$marker))
returns <- c()
for (i in 1:nrow(btc)) {
if (btc[i, 10] == TRUE){
r <- btc[i+30, 5]*100/btc[i, 5]
rr <- r - 100
returns <- c(returns, rr)
}
}
returns <-as.data.frame(returns)
returns <- na.omit(returns)
returns <- returns[returns<1000]
returns <-as.data.frame(returns)
library(ggplot2)
ggplot(returns, aes(returns)) + geom_histogram(binwidth=15,
fill= '#56b4e9', col = '#e69f00',
alpha=0.6) + ggtitle("30 Day $100 Investment Returns for BTC in an Uptrend") + labs(x='Returns', y='Frequency')
For this analysis, we apply a different investment strategy to the same bitcoin market. We only make the $100 investment if we observe an uptrend in the market, with uptrend defined as follows: the daily percentage change in price averaged over a week is greater than 5%. The 5% quantity was selected to 1.) be a sensible qualification of an uptrend and 2.) restrict us to look at about 10% of all trading events. Therefore there were 155 trading events observed in this analysis. Again, $100 is invested when an uptrend is detected and the investment is sold for a return after 30 days. As you can see, the distribution looks completely different. Firstly, any resemblance of a normal distribution is virtually wiped out. There are many returns around the 0 range, and many interspersed, perhaps diminishing, as one ventures into the higher return territory. The negative returns still exist; however, they do not have as strong an effect as in the previous analysis.
Fact: The expected return of this investment strategy in bitcoin is $58.76.
This return is almost double that of the regular investment strategy case, where we are not buying in an uptrend. The important observation here is that bitcoin is “trendy,” that is, if an upward trend exists, it is likely to continue. Let’s now look at the same uptrend strategy applied to the S&P.
spy$daily_pct_change <- spy$close/lag(spy$close) - 1
avg_rate <- c()
for (i in 2:nrow(spy)) {
i <- i
r <- (spy[i, 8] + spy[i+1, 8] + spy[i+2, 8] + spy[+3, 8] + spy[i+4, 8] + spy[i+5, 8] + spy[i+6, 8])/8
avg_rate <- c(avg_rate, r)
}
avg_rate <- avg_rate[!(is.na(avg_rate))]
avg_rate <- c(c(0, 0, 0, 0, 0, 0, 0), avg_rate)
#avg_rate <- c(c(0, 0, 0), avg_rate)
spy$avg_rate <- avg_rate
marker <- ((spy$avg_rate > 0.0015))
spy$marker <- marker
length(which(spy$marker))
returns <- c()
for (i in 1:nrow(spy)) {
if (spy[i, 10] == TRUE){
r <- spy[i+30, 2]*100/spy[i, 2]
rr <- r - 100
returns <- c(returns, rr)
}
}
returns <-as.data.frame(returns)
returns <- na.omit(returns)
print ("Trading events")
print (nrow(returns))
print ("Average return")
print (lapply(returns, mean))
library(ggplot2)
ggplot(returns, aes(returns)) + geom_histogram(binwidth=1,
fill= '#56b4e9', col = '#e69f00',
alpha=0.6) + ggtitle("30 Day $100 Investment Returns for S&P in an Uptrend") + labs(x='Returns', y='Frequency')
This histogram examines the returns of the “Uptrend strategy” applied to the S&P. We invest $100 if the daily percentage change in price over a week is on average greater than a certain percentage (chosen to include the roughly 10% of periods with biggest trends), and then we sell our holdings 30 days after buying. There were 140 trading events in this analysis. As in the bitcoin histogram, the normal distribution has disappeared. One may notice that high returns of close to $10 have gone, while low returns of -$10 remain. Things look bleaker. Indeed, the expected return of this investment strategy is $0.91, as compared to $0.94 in the non-uptrend general strategy. Why is this? Well, short term trends in the stock market are often not strong forecasting indicators. The market’s trends are immune to simple investment strategies such as this one, where we are buying after a week of uptrend and selling later. As a result, unlike bitcoin, the histogram for the S&P in this case has shifted to something less desirable. It is better to invest in the stock market on a less intelligent strategy: buy and sell 30 days later.